package com.tuanzi.loan.business.repository.statistics.impl;

import com.tuanzi.loan.business.repository.statistics.LoanAdvertiseTransFormationRepository;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.vo.request.advertise.AdvertiseTransStatRequest;
import com.tuanzi.loan.vo.response.advertise.AdvertiseTransStatResponse;
import org.apache.commons.lang3.StringUtils;
import org.jooq.impl.DSL;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import java.util.List;

import static com.tuanzi.loan.generated.jooq.loan.tables.LoanAdvertiseTranResult.LOAN_ADVERTISE_TRAN_RESULT;
import static com.tuanzi.loan.generated.jooq.loan.tables.LoanToutiaoAdvertiseTransformation.LOAN_TOUTIAO_ADVERTISE_TRANSFORMATION;


/**
 * @author Simon
 * @date 2019/07/29 14:28
 */
@Repository
public class LoanAdvertiseTransFormationRepositoryImpl extends JOOQBaseRepositoryPlus implements LoanAdvertiseTransFormationRepository {

    private static Long sum;

    protected TableImpl getTable() {
        return LOAN_TOUTIAO_ADVERTISE_TRANSFORMATION;
    }
    @Override
    public Page<AdvertiseTransStatResponse> list(AdvertiseTransStatRequest searchRequest, Pageable pageable) {
        sum = 0L;
        fields(
                LOAN_ADVERTISE_TRAN_RESULT.CDATE,
                LOAN_TOUTIAO_ADVERTISE_TRANSFORMATION.AD_ID,
                LOAN_ADVERTISE_TRAN_RESULT.ADS_PLATFORM_TYPE,
                LOAN_ADVERTISE_TRAN_RESULT.PLATFORM,
                LOAN_ADVERTISE_TRAN_RESULT.CHANNEL,
                DSL.count(LOAN_ADVERTISE_TRAN_RESULT.CHANNEL).as("advertise_count"));
        leftJoin(LOAN_ADVERTISE_TRAN_RESULT, LOAN_ADVERTISE_TRAN_RESULT.PHONE.eq(LOAN_TOUTIAO_ADVERTISE_TRANSFORMATION.MOBNUM));
        isNotNull(LOAN_TOUTIAO_ADVERTISE_TRANSFORMATION.MOBNUM);
        if (1000 != searchRequest.getAdsPlatformType()){
            eq(LOAN_ADVERTISE_TRAN_RESULT.ADS_PLATFORM_TYPE, searchRequest.getAdsPlatformType());
        }
        eq(LOAN_ADVERTISE_TRAN_RESULT.CDATE, searchRequest.getStartDate());
        if(StringUtils.isNotBlank(searchRequest.getChannel()) && StringUtils.isNotEmpty(searchRequest.getChannel())){
            eq(LOAN_ADVERTISE_TRAN_RESULT.CHANNEL, searchRequest.getChannel());
        }
        if(StringUtils.isNotBlank(searchRequest.getPlatform())){
            eq(LOAN_ADVERTISE_TRAN_RESULT.PLATFORM, searchRequest.getPlatform());
        }

        groupBy(LOAN_TOUTIAO_ADVERTISE_TRANSFORMATION.AD_ID);
        groupBy(LOAN_ADVERTISE_TRAN_RESULT.ADS_PLATFORM_TYPE);
        groupBy(LOAN_ADVERTISE_TRAN_RESULT.CDATE);


        return search(pageable, record -> {
            AdvertiseTransStatResponse advertiseTransStatResponse = new AdvertiseTransStatResponse();
            advertiseTransStatResponse.setTime(record.get(LOAN_ADVERTISE_TRAN_RESULT.CDATE));
            advertiseTransStatResponse.setAdid(record.get(LOAN_TOUTIAO_ADVERTISE_TRANSFORMATION.AD_ID,String.class));
            advertiseTransStatResponse.setAdsPlatformType(record.get(LOAN_ADVERTISE_TRAN_RESULT.ADS_PLATFORM_TYPE,String.class));
            advertiseTransStatResponse.setPlatform(record.get(LOAN_ADVERTISE_TRAN_RESULT.PLATFORM,String.class));
            advertiseTransStatResponse.setChannel(record.get(LOAN_ADVERTISE_TRAN_RESULT.CHANNEL,String.class));
            advertiseTransStatResponse.setCount(record.get("advertise_count", Long.class));
            sum += record.get("advertise_count", Long.class)==null? 0L : record.get("advertise_count", Long.class);
            return advertiseTransStatResponse;
        });
    }

    public Long getCountSum() {
        return sum;
    }

    public List<AdvertiseTransStatResponse> getDataList(AdvertiseTransStatRequest searchRequest) {
        return null;
    }
}
